package com.zmn.oms.admin.controller.work.modify;

import com.jd.open.api.sdk.internal.JSON.JSON;
import com.zmn.oms.admin.controller.work.WorkBaseController;
import com.zmn.oms.common.annotation.PageRequestCas;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.constant.OrderStatusConsts;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.dto.work.modify.must.OrderAssignBackDTO;
import com.zmn.oms.model.vo.work.OrderAssignBackVO;
import com.zmn.oms.zmn.business.interfaces.order.OrderVerifyBService;
import com.zmn.oms.zmn.business.interfaces.work.OrderWorkAssignBackBService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Objects;

/**
 * 类描述：分单撤回
 *
 * @author liuying
 * @date 2019/05/09 16:06
 */
@Controller
@RequestMapping("/work/modify")
public class WorkAssignBackController extends WorkBaseController {

    @Autowired
    private OrderWorkAssignBackBService orderWorkAssignBackBService;
    @Autowired
    private OrderVerifyBService orderVerifyBService;

    /**
     * 分单撤回
     */
    @RequestMapping(value = "/assign/back")
    @PageRequestCas(type = OrderConsts.ORDER_OP_TYPE_ASSIGN_BACK)
    public ModelAndView assignBack(Long orderId, Long workId) {

        ModelAndView mv = new ModelAndView("work/modify/assignback");

        OrderAssignBackVO assignBackVO = orderWorkAssignBackBService.getAssignBackVO(orderId, workId);
        String tips = orderVerifyBService.getAmountTips(orderId, workId);

        mv.addObject("tips", tips);
        mv.addObject("assignBackVO", assignBackVO);

        return mv;
    }

    /**
     * 保存分单撤回
     */
    @RequestMapping(value = "/save/assign/back")
    @ResponseBody
    public ResultDTO saveAssignBack(@RequestBody OrderAssignBackDTO orderAssignBackDTO) {

        logger.debug("保存分单撤回：【{}】", JSON.toString(orderAssignBackDTO));

        try {
            if (Objects.isNull(orderAssignBackDTO.getOrderId()) || Objects.isNull(orderAssignBackDTO.getWorkId())) {
                return ResultDTO.fail("非法请求");
            }
            orderAssignBackDTO.setStatus(OrderStatusConsts.WORK_STATUS_CONFIRM);
            orderAssignBackDTO.setResultStatus(OrderStatusConsts.WORK_RESULT_DOING);
            orderWorkAssignBackBService.saveAssignBack(orderAssignBackDTO);
            return ResultDTO.success();
        } catch (OmsBaseException e) {
            e.printStackTrace();
            return ResultDTO.fail(e.getMessage());
        }
    }

}
